package com.zhiwang.planet.mvp.p

import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.mvp.v.FictionCompleteView
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.util.androidSchedule
import com.zhiwang.planet.util.responseFunc
import com.uber.autodispose.AutoDispose
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.zhiwang.planet.mvvm.model.entity.main.VideoClipTaskResult
import javax.inject.Inject

@ActivityScope
class EntertainPresenter @Inject constructor(var restApi: RestApi, var scopeProvider: AndroidLifecycleScopeProvider) {


    lateinit var fictionReadingCompleteView: FictionCompleteView

    fun postFictionReadingTime(time: Int) {
        restApi.postFictionReadingTime("", time, 1)
                .androidSchedule()
                .responseFunc()
                .`as`(AutoDispose.autoDisposable(scopeProvider))
                .subscribe({
                    fictionReadingCompleteView.onFictionTimePostSuccess(it)
                }, {
                    fictionReadingCompleteView.onError(it)
                }, {
                    //                    fictionReadingCompleteView.onFictionTimePostSuccess()
                })

    }

    fun postHeadlineReadingTime(url: String, time: Int) {
        restApi.postFictionReadingTime(url, time, 2)
                .androidSchedule()
                .responseFunc()
                .`as`(AutoDispose.autoDisposable(scopeProvider))
                .subscribe({
                    fictionReadingCompleteView.onFictionTimePostSuccess(it)
                }, {
                    fictionReadingCompleteView.onError(it)
                }, {
                    //                    fictionReadingCompleteView.onFictionTimePostSuccess()
                })
    }

    fun postVideoClipsTime(onHandleVideoTask: ((VideoClipTaskResult) -> Unit), onError: (Throwable) -> Unit) {

        restApi.postVideoClipTime()
                .androidSchedule()
                .responseFunc()
                .`as`(AutoDispose.autoDisposable(scopeProvider))
                .subscribe({

                    onHandleVideoTask.invoke(it/*it.msg, it.reward, it.countDownTime, it.completed*/)
                }, {

                    onError(it)
                }, {
                    //                    fictionReadingCompleteView.onFictionTimePostSuccess()
                })
    }
}